package com.itheima.service.impl;

import com.itheima.mapper.ReportMapper;
import com.itheima.pojo.ClazzCount;
import com.itheima.pojo.ClazzCountOption;
import com.itheima.pojo.JobOption;
import com.itheima.service.ClazzService;
import com.itheima.service.ReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;
    @Autowired
    private ClazzService clazzService;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<HashMap<String, Object>> getEmpGenderData() {
        List<HashMap<String, Object>> list = reportMapper.getEmpGenderData();
        // 1,
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public JobOption getEmpJobData() {
      List<HashMap<String,Object>> jobData= reportMapper.getEmpJobData();

        // System.out.println("jobData = " + jobData);

        // for (HashMap<String, Object> jobDatum : jobData) {
        //     System.out.println("jobDatum = " + jobDatum);
        // }

        // 为了方便操作List集合 把 list 转为stream流,然后循环遍历，
        // 在遍历的过程中拿到对应的key,最后按照顺序把这些key 收集到集合中即可

        List<Object> jobNameList = jobData.stream().map((HashMap hashMap) -> {
            Object jobName = hashMap.get("jobName");
            return jobName;
        }).collect(Collectors.toList());

        List<Object> countList = jobData.stream().map((HashMap hashMap) -> {
            Object count = hashMap.get("count");
            return count;
        }).collect(Collectors.toList());

        JobOption jobObj = new JobOption(jobNameList,countList);
        return jobObj;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public ClazzCount getClazzCount() {

      List<Map>  list = clazzService.getClazzCount();

        List<Object> name = list.stream().map((item) -> item.get("name")).collect(Collectors.toList());
        List<Object> value = list.stream().map((item) -> item.get("value")).collect(Collectors.toList());

        ClazzCount clazzCount = new ClazzCount(name,value);
        return clazzCount;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<Map> getStudentDegreeData() {
      List<Map>  list =  clazzService.getStudentDegreeData();

        System.out.println("list = " + list);
        

        return list;
    }
}
